<?php
//获取主题名
	function themeUsing()
	{
		$conn = new mysqlClass();
		$query = "SELECT value FROM record WHERE name='themeUsing';";
		$conn->query($query);
		$conn->fetch(0);
		return $conn->data['value'];
	}

//头部包含
	function getHeader()
	{
		include(THEMEPATH.'header.php');
	}

//尾部包含
	function getFooter()
	{
		include(THEMEPATH.'footer.php');
	}

//边栏包含
	function getPanel()
	{
		include(THEMEPATH.'panel.php');
	}

//获取相册首图(封面)
	function getAlbumFirstImageUrl($aid)
	{
		$conn = new mysqlClass();
		$query = "SELECT imageName From images WHERE imageOwner='{$aid}' LIMIT 0,1;";
		$conn->query($query);
		$conn->fetch(0);
		return UPLOADURL.$conn->data['imageName'];
	}

//获取邀请码
	function getInviteCode()
	{
		$code = md5(time() + $_SESSION['userName']);
		$conn = new mysqlClass();
		$query = "INSERT INTO rancode (action,code) VALUES ('regInvite','{$code}');";
		$conn->query($query);
		return $code;
	}
	
//检查邀请码
	function chkInviteCode($inviteCode)
	{
		$conn = new mysqlClass();
		$query = "SELECT action FROM rancode WHERE code='{$inviteCode}' AND action='regInvite';";
		$conn->query($query);
		if ($conn->row == 0)
		{
			return FALSE;
		}
		else 
		{
			$query = "DELETE FROM rancode WHERE code='{$inviteCode}' AND action='regInvite';";
			$conn->query($query);
			return TRUE;
		}
	}
	
//获取新uid以及用户名检测
	function getNewUidAndCheckName($userName,$returnUid = TRUE)
	{
		$conn = new mysqlClass();
		$query = "SELECT uid FROM users WHERE userName='{$userName}';";
		$conn->query($query);
		if ($conn->row == 1) return 0;
		//返回0为已存在该用户
		if ($returnUid)
		{
			$query = "SELECT * FROM record WHERE name='userAmount';";
			$conn->query($query);
			$conn->fetch(0);
			//返回新uid
			return $conn->data['value'] + 1;
		}
		else
		{
			return -1;
		}
	}
	
//用户注册
	function userReg($userName,$userPwd,$userRePwd,$userEmail,$inviteCode)
	{
		if ($userPwd != $userRePwd) return 5;
		if (!chkInviteCode($inviteCode)) return 6;
		$name = str_replace(" ","",$userName);
		$data['uid'] = getNewUidAndCheckName($name);
		if ($data['uid'] == 0) return 1;
		//wait: 检查非法字符
		$data['userName'] = $userName;
		$data['userPwd'] = md5($userPwd);
		//wait: 检查Email格式
		$data['userEmail'] = $userEmail;
		
		$reg = new userProf($data);
		if ($reg->create())
		{
			return 0;
		}
	}

//用户登录
	function login($userName,$userPwd,$time='86400')
	{
		$conn = new mysqlClass();
		$query = "SELECT userPwd,uid FROM users WHERE userName='{$userName}';";
		$conn->query($query);
		$conn->fetch(0);
		$pwd = md5($userPwd);
		if ($pwd == $conn->data['userPwd'])
		{
			$t = time() + $time;
			setcookie("userName",$userName,$t);
			setcookie("authPassed",$pwd,$t);
			setcookie("uid",$conn->data['uid'],$t);
			chkLoginStatus();
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

//检查登录状态, 设置session
	function chkLoginStatus($do = 'CHECK')
	{
		if ($do == 'SET')
		{
			$_SESSION['userName'] = $_COOKIE['userName'];
			$_SESSION['authPassed'] = 'TRUE';
			$_SESSION['uid'] = $_COOKIE['uid'];
		}
		else 
		{
			if (isset($_COOKIE['uid']))
			{
				$conn = new mysqlClass();
				$query = "SELECT userPwd FROM users WHERE userName='{$_COOKIE['userName']}';";
				$conn->query($query);
				$conn->fetch(0);
				$pwd = $_COOKIE['authPassed'];

				if ($pwd == $conn->data['userPwd'])
				{
					$_SESSION['userName'] = $_COOKIE['userName'];
					$_SESSION['authPassed'] = 'TRUE';
					$_SESSION['uid'] = $_COOKIE['uid'];
				}
			}
		}
	}
	
//用户登出
	function logout()
	{
		setcookie("userName","",time() - 1);
		setcookie("authPassed","",time() - 1);
		setcookie("uid","",time() - 1);
		unset($_COOKIE['userName']);
		unset($_COOKIE['authPassed']);
		unset($_COOKIE['uid']);
		session_destroy();
	}

//标签分离
	function userTagSeparate($query)
	{
		return explode(',', $query);
	}

	function userCompetitionSeparate($query)
	{
		return explode('-', $query);
	}

//返回文章类型
	function readablePostType($query)
	{
		switch ($query)
		{
			case 'official':
				return '比赛资料';
				break;
			case 'discuss':
				return '讨论';
				break;
			default:
				return $query;
		}
	}
	
	function getMemberLink($query,$allInOne=TRUE)
	{
		$arr = explode(',', $query);
		$string = '';
		if ($allInOne)
		{
			foreach ($arr as $i)
			{
				$string = $string."<a href='?p=viewUser&uid={$i}'>".userProf::getUsernameByUid($i)."</a> ";
			}
			return $string;
		}
		else 
		{
			$count = 0;
			foreach ($arr as $i)
			{
				$string[$count] = "<a href='?p=viewUser&uid={$i}'>".userProf::getUsernameByUid($i)."</a> ";
				$count++;
			}
		}
	}
	
	function getEditor()
	{
		echo "<link rel='stylesheet'	href='".PLUGINURL."tinyEditor/style.css' />
			  <script type='text/javascript'	src='".PLUGINURL."tinyEditor/tinyeditor.js'></script>
				<script type='text/javascript'>
				var instance =new TINY.editor.edit('editor',{
				id:'tinyInput',
				width:584,
				height:175,
				cssclass:'te',
				controlclass:'tecontrol',
				rowclass:'teheader',
				dividerclass:'tedivider',
				controls:['bold','italic','underline','strikethrough','|','subscript','superscript','|',
			  		'orderedlist','unorderedlist','|','outdent','indent','|','leftalign',
			  		'centeralign','rightalign','blockjustify','|','unformat','|','undo','redo','n',
			  		'font','size','style','|','image','hr','link','unlink','|','cut','copy','paste','print'],
				footer:true,
				fonts:['Verdana','Arial','Georgia','Trebuchet MS'],
				xhtml:true,
				cssfile:'style.css',
				bodyid:'editor',
				footerclass:'tefooter',
				toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggle'},
				resize:{cssclass:'resize'}
				});
			</script> </textarea>";
	}
	
	function listFile($dir)
	{
	    if(is_dir($dir))
		{  
			if($dirHandle = opendir($dir))
			{
				$i = 0;
				while ($fileName = readdir($dirHandle)) 
				{
					if($fileName=='.' || $fileName == '..' || $fileName == 'index.php')
					{
						continue;
					}
					else
					{
						$arr[$i++] = $fileName;
					}
				}
			}
		}
		return $arr;
	}
?>